<?php

require_once 'modules/content/models/Content.php';
require_once 'modules/content/models/ContentCategory.php';
require_once 'modules/comment/models/Comment.php';
//require_once  'libs/Zend/Controller/Action.php';



class commentSticker extends Nine_Sticker
{
	
	public function run()
	{
		$objComment = new Models_Comment();
        $errors = array();
//		echo "<pre>";print_r($_POST);die;
		$gid = Nine_Controller_Front::getInstance()->getRequest()->getParam('id',false);
//		echo "<pre>";print_r($gid);die;
            $objContent = new Models_Content();
            $content = $objContent->getContentByGid($gid);
            $data = Nine_Controller_Front::getInstance()->getRequest()->getParam('data', false);
//            
//            
	$errors = array();
//	echo "<pre>";print_r($this->session->commentMessage);die;
//echo "<pre>";print_r($_SERVER["PHP_SELF"]);die;
//echo "<pre>";print_r($_SESSION);
//session_start();
//echo "<pre>";print_r(isset($_POST["submit"]));
//if (1 == isset($_POST["submit"]))
//{
//	echo "<pre>";print_r($_POST);
//echo"==================================";
//echo "<pre>";print_r($_SESSION);
//echo "=================================";
//    if ($_POST["formid"] == $_SESSION["formid"] && $_SESSION["formid"])
//    {
//        $_SESSION["formid"] = '';
        if (false != $data) {
            /**
             * Insert new content
             */
            
            try {

                  $data['enabled'] = 1;
                  $data['content_id'] = $content['content_id'];
                  $data['content_gid'] = $content['content_gid']; 
                  $data['created_date'] = time();
                  $data['lang_id'] = $content['lang_id'];           
                               
                  $objComment->insert($data);
//                unset($_POST);
                  
                                
                /**
                 * Message
                 */
                $this->session->commentMessage =  array(
                           'success' => true,
                           'message' => Nine_Language::translate('Comment is created successfully.')
                        );
//                 Nine_Controller_Front::getInstance()->_redirect(Nine_Controller_Front::getInstance()->_getCallBackUrl ());
                $link = Nine_Registry::getConfig('liveSite')."content/index/detail/id/".$gid;
                header("Location: {$link}");
            } catch (Exception $e) {
                $errors = array('main' => Nine_Language::translate('Can not insert into database now'));
            }
        }
//    }
//    else{
//    	$this->session->commentMessage =  array(
//                           'success' => false,
//                           'message' => Nine_Language::translate('You must reset to have more submission .')
//                        );
//    }
//}
//else
//{
////	echo "here";
//    $_SESSION["formid"] = md5(rand(0,10000000));
////     echo "<pre>";print_r($_SESSION);   
//	 
//	
//}
    
        $allComments = $objComment->getAllEnabledCommentById($content['content_id'],array('comment_id DESC'));
//        echo "<pre>";print_r($allComments);die;
        $this->view->comments = $allComments;
        $this->view->sm = $_SESSION["formid"];
         $this->view->commentMessage = $this->session->contactMessage;
        $this->session->commentMessage = null;
	}
}